//------------------------------------------------------------------------------//
// 
// MODULE   : camera.cpp
// 
// PURPOSE  : CCamera - Implementation
// 
// CREATED  : 6/27/2002
// 
// (c) 2002 LithTech, Inc.  All Rights Reserved
// 
//------------------------------------------------------------------------------//

#include "camera.h"
#include <iltclient.h>
#include <ltobjectcreate.h>
#include <stdexcept>
#include <pageInfoGetter.hpp>

camera::~camera() {
    if(m_hObject) {
        g_pLTClient->RemoveObject(m_hObject);
        m_hObject = NULL ;
    }
}


LTVector camera::position() const {
    LTVector pos ;
    g_pLTClient->GetObjectPos(m_hObject, &pos) ;
    return pos ;
}

LTRotation camera::rotation() const {
    LTRotation rot ;
    g_pLTClient->GetObjectRotation(m_hObject, &rot) ;
    return rot ;
}

void camera::set_position(const LTVector& pos) {
    g_pLTClient->SetObjectPos(m_hObject, &pos) ;
}

void camera::set_rotation(const LTRotation& rot) {
    g_pLTClient->SetObjectRotation(m_hObject, &rot) ;
}

void camera::set_position_and_rotation(const LTVector& pos, const LTRotation& rot) {
    g_pLTClient->SetObjectPosAndRotation(m_hObject, &pos, &rot) ;
}
//----------------------------------------------------------------------------
// LTRESULT CCamera::CreateCamera()
//
//-----------------------------------------------------------------------------
camera_ptr camera::create_camera()
{
    
	uint32 nWidth, nHeight;
	ObjectCreateStruct objCreate;
	
	//	Get our screen dimensions, for the camera rectangle
	g_pLTClient->GetSurfaceDims(g_pLTClient->GetScreenSurface(), &nWidth, &nHeight);
	
	//	Initialize our object creation structure
	objCreate.Clear();
	objCreate.m_ObjectType = OT_CAMERA;
    HLOCALOBJ object = g_pLTClient->CreateObject(&objCreate);
	
	if (NULL == object)
      throw std::runtime_error("could not create camera object") ;
  
	
	g_pLTClient->SetCameraRect(object, false, 0, 0, nWidth, nHeight);
	
	//	This is an fov of 90 degrees
	float fFovX = MATH_PI / 3.0f;
	float fFovY = (fFovX * nHeight) / nWidth;
	g_pLTClient->SetCameraFOV(object, fFovX, fFovY);
	
  

	camera_ptr cam(new camera(object)) ;
    cam->m_debugMoveSpeed = Utility::page_info_getter::Instance().get<float>("debug", "camera", "speed", 50.0f) ;


  return cam ;
}


bool  camera::set_camera_rect(float x, float y,float nWidth,float nHeight)
{
     if(m_hObject == NULL)
		 return false;
	 else
	 {
		 g_pLTClient->SetCameraRect(m_hObject, false, x, y, nWidth, nHeight);
		 return true;
	 }
}